package com.peng.leetcode.heap;

import java.util.PriorityQueue;

/**
 * LastStoneWeight
 *
 * @author: lupeng6
 * @create: 2020/12/11 17:21
 */
public class LastStoneWeight {

    public static void main(String[] args) {
        int[] arr = {2, 7, 4, 1, 8, 1};
        System.out.println(new Solution().lastStoneWeight(arr));
    }

    static class Solution {
        public int lastStoneWeight(int[] stones) {
            PriorityQueue<Integer> queue = new PriorityQueue<>(stones.length, (a, b) -> b - a);
            for (int stone : stones) {
                queue.offer(stone);
            }

            while (queue.size() > 1) {
                Integer first = queue.poll();
                Integer second = queue.poll();
                if (first.equals(second)) {
                    continue;
                }
                queue.offer(first - second);
            }

            if (queue.isEmpty()) {
                return 0;
            }

            return queue.poll();
        }
    }
}
